package com.zhongge.servlet;

import com.zhongge.dao.UserDao;
import com.zhongge.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName UserListServlet
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
@WebServlet("/user-list")
public class UserListServlet extends HttpServlet {
    UserDao userDao = new UserDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");

        List<User> users = userDao.getAllUsers();
        req.setAttribute("users",users);
        if (users == null) {
            req.setAttribute("isNull","数据为空");
        } else {
            req.getRequestDispatcher("user-list.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
